package sunhua.controller;

import mysite.mysite1.sunhua.Mapper.DepartmentMapper;
import mysite.mysite1.sunhua.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
public class HelloCronller{
    @Autowired
    DataSource dataSource;

    @Autowired
    DepartmentMapper departmentMapper;

    @RequestMapping("/hello")
    public String hello() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        String s = "select * from t_logs";
        PreparedStatement preparedStatement = connection.prepareStatement(s);
        ResultSet resultSet = preparedStatement.executeQuery();
        return "hello world";
    }

    // 查询全部部门
    @GetMapping("/getDepartments")
    public List<Department> getDepartments() {
        return departmentMapper.getDepartments();
    }

    // 查询全部部门
    @GetMapping("/getDepartment/{id}")//http://123.57.145.188:8081/getDepartment/1
    public Department getDepartment(@PathVariable("id") Integer id) {
        return departmentMapper.getDepartment(id);
    }
}
